unit uFrmCageInventory;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DB, OracleData, ufrmprintcageclose, DBGrids, gnugettext, strutils;

type
  TFrmCageInventory = class(TForm)
    Button1: TButton;
    Button2: TButton;
    stg_CashInventory: TStringGrid;
    lbl_InventoryName: TLabel;
    stg_SumCashValues: TStringGrid;
    lbl_ClosingCash: TLabel;
    stg_ChipInventory: TStringGrid;
    Label2: TLabel;
    Label3: TLabel;
    stg_SumChipValues: TStringGrid;
    lbl_ClosingChips: TLabel;
    lbl_OpeningChips: TLabel;
    lbl_OpeningCash: TLabel;
    lbl_TotalInventory: TLabel;
    lbl_TotalCash: TLabel;
    lbl_TotalChips: TLabel;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure stg_CashInventorySelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure stg_CashInventorySetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
    procedure stg_CashInventoryDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure stg_CashInventoryKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure stg_ChipInventoryKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure stg_ChipInventorySelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure stg_ChipInventorySetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
    procedure stg_SumCashValuesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure stg_SumChipValuesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
    procedure CalcCash();
  public
    { Public declarations }
    IsOpening: Boolean;
  end;

var
  FrmCageInventory: TFrmCageInventory;

implementation

uses uFrmMain, umainutils, uutils, usqls;
{$R *.dfm}

procedure TFrmCageInventory.Button1Click(Sender: TObject);
var
  I: Integer;
  recordID, cid, ci: STRING;
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  recordID := InsertDataIntoDB('CAGE_INVENTORY', 'DATETIME,CLOSING,OPENING', '(SELECT SYSDATE FROM DUAL),' + uutils.BtoStr(not IsOpening) + ',' + uutils.BtoStr(IsOpening));
  for I := 2 to stg_CashInventory.RowCount - 1 do
  begin
    cid := stg_CashInventory.Cells[0, I];
    ci := stg_CashInventory.Cells[ColIdx, I];
    if ci <> '' then
    begin
      InsertDataIntoDB('CAGE_INVENTORY_ITEMS', 'CASINO_INVENTORY_ID,VALUE,CAGE_INVENTORY_ID', cid + ',' + ci + ',' + recordID);
    end;
  end;
  // ---
  for I := 2 to stg_ChipInventory.RowCount - 1 do
  begin
    cid := stg_ChipInventory.Cells[0, I];
    ci := stg_ChipInventory.Cells[ColIdx, I];
    if ci <> '' then
    begin
      InsertDataIntoDB('CAGE_INVENTORY_ITEMS', 'CASINO_INVENTORY_ID,VALUE,CAGE_INVENTORY_ID', cid + ',' + ci + ',' + recordID);
    end;
  end;
  if not IsOpening then
  begin
    FrmPrintCageClose.SetData(recordID, OraSessMainUtils);
  end;
  ModalResult := mrOK;
end;

procedure TFrmCageInventory.Button2Click(Sender: TObject);
begin
  ModalResult := mrCancel;
  close;
end;

procedure TFrmCageInventory.CalcCash;
var
  I: Integer;
  Cash: double;
  CashOpening, CashClosing, ChipOpening, ChipClosing: double;
begin
  Cash := 0;
  for I := 2 to stg_CashInventory.RowCount - 1 do
  begin
    if uutils.IsStrANumber(stg_CashInventory.Cells[3, I]) and (stg_CashInventory.Cells[3, I] <> '') then
      Cash := Cash + (StrToFloat(stg_CashInventory.Cells[2, I]) * StrToFloat(stg_CashInventory.Cells[3, I]));
  end;
  lbl_OpeningCash.Caption := _('Open Cash Value') + ': ' + FloatToStrF(Cash, ffNumber, 10, 2) + ' ' + Currency_Symbol;
  CashOpening := Cash;
  ///
  Cash := 0;
  for I := 2 to stg_ChipInventory.RowCount - 1 do
  begin
    if uutils.IsStrANumber(stg_ChipInventory.Cells[3, I]) and (stg_ChipInventory.Cells[3, I] <> '') then
      Cash := Cash + (StrToFloat(stg_ChipInventory.Cells[2, I]) * StrToFloat(stg_ChipInventory.Cells[3, I]));
  end;
  lbl_OpeningChips.Caption := _('Open Chip Value') + ': ' + FloatToStrF(Cash, ffNumber, 10, 2) + ' ' + Currency_Symbol;
  ChipOpening := Cash;
  //
  if stg_CashInventory.ColCount = 5 then
  begin
    Cash := 0;
    for I := 2 to stg_CashInventory.RowCount - 1 do
    begin
      if uutils.IsStrANumber(stg_CashInventory.Cells[4, I]) and (stg_CashInventory.Cells[4, I] <> '') then
        Cash := Cash + (StrToFloat(stg_CashInventory.Cells[2, I]) * StrToFloat(stg_CashInventory.Cells[4, I]));
    end;
    lbl_ClosingCash.Caption := _('Close Cash Value') + ': ' + FloatToStrF(Cash, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    CashClosing := Cash;
    ///
    Cash := 0;
    for I := 2 to stg_ChipInventory.RowCount - 1 do
    begin
      if uutils.IsStrANumber(stg_ChipInventory.Cells[4, I]) and (stg_ChipInventory.Cells[4, I] <> '') then
        Cash := Cash + (StrToFloat(stg_ChipInventory.Cells[2, I]) * StrToFloat(stg_ChipInventory.Cells[4, I]));
    end;
    lbl_ClosingChips.Caption := _('Close Chip Value') + ': ' + FloatToStrF(Cash, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    ChipClosing := Cash;
  end;
  // ---
  if stg_CashInventory.ColCount = 5 then // closing
  begin
    lbl_TotalCash.Caption := _('Total Cash') + ': ' + FloatToStrF(CashClosing - CashOpening, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    lbl_TotalChips.Caption := _('Total Chips') + ': ' + FloatToStrF(ChipClosing - ChipOpening, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    lbl_TotalInventory.Caption := _('Total') + ': ' + FloatToStrF((ChipClosing - ChipOpening) + (CashClosing - CashOpening), ffNumber, 10, 2) + ' ' + Currency_Symbol;
    // ---
    if CashClosing - CashOpening < 0 then
      lbl_TotalCash.Font.Color := clred
    else
      lbl_TotalCash.Font.Color := clGreen;
    if ChipClosing - ChipOpening < 0 then
      lbl_TotalChips.Font.Color := clred
    else
      lbl_TotalChips.Font.Color := clGreen;
    if (CashClosing - CashOpening) + (ChipClosing - ChipOpening) < 0 then
      lbl_TotalInventory.Font.Color := clred
    else
      lbl_TotalInventory.Font.Color := clGreen;
  end
  else // opening
  begin
    lbl_TotalCash.Caption := _('Total Cash') + ': ' + FloatToStrF(CashOpening, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    lbl_TotalChips.Caption := _('Total Chips') + ': ' + FloatToStrF(ChipOpening, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    lbl_TotalInventory.Caption := _('Total') + ': ' + FloatToStrF((ChipOpening) + (CashOpening), ffNumber, 10, 2) + ' ' + Currency_Symbol;
    // ---
    lbl_TotalCash.Font.Color := clGreen;
    lbl_TotalChips.Font.Color := clGreen;
    lbl_TotalInventory.Font.Color := clGreen;
  end;
end;

procedure TFrmCageInventory.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if ModalResult <> mrOK then
    ModalResult := mrCancel;
end;

procedure TFrmCageInventory.FormShow(Sender: TObject);
var
  Cash: double;
begin
  /// /
  FrmMain.OracleQuery1.close;
  FrmMain.OracleQuery1.SQL.Clear;
  if IsOpening then
  begin
    FrmMain.OracleQuery1.SQL.Text := SQL_CasinoCashInventory;
  end
  else
  begin
    FrmMain.OracleQuery1.SQL.Text := 'select X1.ID, DENOM_TYPE, DENOM_VALUE, VALUE, NULL AS OPEN_ITEMS from (' + SQL_CasinoCashInventory +
      ') x1 left join GET_LAST_CAGE_INV_ITEMS x2 on x2.casino_inventory_id=x1.id';
  end;
  FrmMain.OracleQuery1.Execute;
  FrmMain.SetGridData(stg_CashInventory, FrmMain.OracleQuery1, false);
  stg_CashInventory.ColCount := 4;
  stg_SumCashValues.ColCount := 4;
  stg_CashInventory.Cells[3, 1] := _('Open Items');
  stg_CashInventory.ColWidths[0] := -1;
  stg_CashInventory.ColWidths[1] := 60;
  stg_CashInventory.ColWidths[2] := 70;
  stg_CashInventory.ColWidths[3] := 70;
  stg_SumCashValues.ColWidths[0] := -1;
  stg_SumCashValues.ColWidths[1] := 60;
  stg_SumCashValues.ColWidths[2] := 70;
  stg_SumCashValues.ColWidths[3] := 70;
  /// /
  FrmMain.OracleQuery1.close;
  FrmMain.OracleQuery1.SQL.Clear;
  if IsOpening then
  begin
    FrmMain.OracleQuery1.SQL.Text := SQL_CasinoChipInventory;
  end
  else
  begin
    FrmMain.OracleQuery1.SQL.Text := 'select X1.ID, DENOM_TYPE, DENOM_VALUE, VALUE AS OPEN_ITEMS, NULL from (' + SQL_CasinoChipInventory +
      ') x1 left join GET_LAST_CAGE_INV_ITEMS x2 on x2.casino_inventory_id=x1.id';
  end;
  FrmMain.OracleQuery1.Execute;
  FrmMain.SetGridData(stg_ChipInventory, FrmMain.OracleQuery1, false);
  stg_ChipInventory.ColCount := 4;
  stg_SumChipValues.ColCount := 4;
  stg_ChipInventory.Cells[3, 1] := _('Open Items');
  stg_ChipInventory.ColWidths[0] := -1;
  stg_ChipInventory.ColWidths[1] := 60;
  stg_ChipInventory.ColWidths[2] := 70;
  stg_ChipInventory.ColWidths[3] := 70;
  stg_SumChipValues.ColWidths[0] := -1;
  stg_SumChipValues.ColWidths[1] := 60;
  stg_SumChipValues.ColWidths[2] := 70;
  stg_SumChipValues.ColWidths[3] := 70;
  stg_SumCashValues.Cells[3, 0] := SumColGridData(stg_CashInventory, 3, 2, stg_CashInventory.RowCount);
  stg_SumChipValues.Cells[3, 0] := SumColGridData(stg_ChipInventory, 3, 2, stg_ChipInventory.RowCount);
  CalcCash;
  if IsOpening then
  begin
    FrmCageInventory.lbl_InventoryName.Caption := _('CAGE OPENING INVENTORY');
    lbl_ClosingCash.Visible := false;
    lbl_ClosingChips.Visible := false;
    lbl_OpeningChips.Visible := true;
    lbl_OpeningCash.Visible := true;
  end
  else
  begin
    Cash := 0;
    lbl_OpeningCash.Caption := _('Cash Value') + ': ' + FloatToStrF(Cash, ffNumber, 10, 2) + ' ' + Currency_Symbol;
    lbl_ClosingCash.Visible := true;
    lbl_ClosingChips.Visible := true;
    lbl_OpeningChips.Visible := true;
    lbl_OpeningCash.Visible := true;
    FrmCageInventory.lbl_InventoryName.Caption := _('CAGE CLOSING INVENTORY');
    stg_ChipInventory.ColCount := 5;
    stg_CashInventory.ColCount := 5;
    stg_SumCashValues.ColCount := 5;
    stg_SumChipValues.ColCount := 5;
    stg_ChipInventory.Cells[4, 1] := _('Close Items');
    stg_CashInventory.Cells[4, 1] := _('Close Items');
    stg_SumChipValues.ColWidths[4] := 80;
    stg_SumCashValues.ColWidths[4] := 80;
    stg_CashInventory.ColWidths[4] := 80;
    stg_ChipInventory.ColWidths[4] := 80;
    stg_SumCashValues.Cells[4, 0] := SumColGridData(stg_CashInventory, 4, 2, stg_CashInventory.RowCount);
    stg_SumChipValues.Cells[4, 0] := SumColGridData(stg_ChipInventory, 4, 2, stg_ChipInventory.RowCount);
    CalcCash();
  end;
end;

procedure TFrmCageInventory.stg_CashInventoryDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  s: string;
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
  s := (Sender as TStringGrid).Cells[ACol, ARow];
  if (ACol in [3, 4]) and (ARow > TStringGrid(Sender).FixedRows - 1) then
  begin
    if ACol = 3 then
      TStringGrid(Sender).Canvas.Brush.Color := $00DA8D8B
    else
      TStringGrid(Sender).Canvas.Brush.Color := $00CA95FF; // CLOSING
    TStringGrid(Sender).Canvas.Font.Color := clBlack;
    TStringGrid(Sender).Canvas.Font.Style := [fsBold];
    if (gdSelected in State) and (ACol = ColIdx) then
    begin
      TStringGrid(Sender).Canvas.Font.Color := clBlack;
      TStringGrid(Sender).Canvas.Brush.Color := clWhite;
    end;
    TStringGrid(Sender).Canvas.fillrect(Rect);
    DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s), Rect, dt_wordbreak or DT_CENTER);
  end;
end;

procedure TFrmCageInventory.stg_CashInventoryKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    if stg_CashInventory.Row < stg_CashInventory.RowCount - 1 then
    begin
      stg_CashInventory.Row := stg_CashInventory.Row + 1;
    end;
  end;
end;

procedure TFrmCageInventory.stg_CashInventorySelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  if ACol = ColIdx then
  begin
    stg_CashInventory.Options := stg_CashInventory.Options + [goEditing];
  end
  else
  begin
    stg_CashInventory.Options := stg_CashInventory.Options - [goEditing];
  end;
  stg_CashInventory.Repaint;
end;

procedure TFrmCageInventory.stg_CashInventorySetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
var
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  stg_SumCashValues.Cells[ColIdx, 0] := SumColGridData(stg_CashInventory, ColIdx, 2, stg_CashInventory.RowCount);
  if not IsStrANumber(stg_CashInventory.Cells[ColIdx, ARow]) then
  begin
    stg_CashInventory.Cells[ColIdx, ARow] := LeftStr(stg_CashInventory.Cells[ColIdx, ARow], Length(stg_CashInventory.Cells[ColIdx, ARow]) - 1);
  end;
  CalcCash;
end;

procedure TFrmCageInventory.stg_ChipInventoryKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
  begin
    if stg_ChipInventory.Row < stg_ChipInventory.RowCount - 1 then
    begin
      stg_ChipInventory.Row := stg_ChipInventory.Row + 1;
    end;
  end;
end;

procedure TFrmCageInventory.stg_ChipInventorySelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  if ACol = ColIdx then
  begin
    stg_ChipInventory.Options := stg_ChipInventory.Options + [goEditing];
  end
  else
  begin
    stg_ChipInventory.Options := stg_ChipInventory.Options - [goEditing];
  end;
  stg_ChipInventory.Refresh;
end;

procedure TFrmCageInventory.stg_ChipInventorySetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
var
  ColIdx: Integer;
begin
  if IsOpening then
    ColIdx := 3
  else
    ColIdx := 4;
  stg_SumChipValues.Cells[ColIdx, 0] := SumColGridData(stg_ChipInventory, ColIdx, 2, stg_ChipInventory.RowCount);
  if not IsStrANumber(stg_ChipInventory.Cells[ColIdx, ARow]) then
  begin
    stg_ChipInventory.Cells[ColIdx, ARow] := LeftStr(stg_ChipInventory.Cells[ColIdx, ARow], Length(stg_ChipInventory.Cells[ColIdx, ARow]) - 1);
  end;
  CalcCash;
end;

procedure TFrmCageInventory.stg_SumCashValuesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

procedure TFrmCageInventory.stg_SumChipValuesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

end.
